home *** CD-ROM | disk | FTP | other *** search
- Path: news.bridge.net!news
- From: David Byrden <100101.2547@compuserve.com>
- Newsgroups: comp.lang.c++
- Subject: Re: Need help with overloading "<<"
- Date: 22 Jan 1996 08:47:43 GMT
- Organization: self-employed
- Message-ID: <4dvivf$460@news.bridge.net>
- References: <4duet0$ju9@wegener.ems.psu.edu> <yang-2101962117310001@hobbes.ece.uiuc.edu>
- NNTP-Posting-Host: ppp-mia1-55.bridge.net
- Mime-Version: 1.0
- Content-Type: text/plain; charset=us-ascii
- Content-Transfer-Encoding: 7bit
- X-Mailer: Mozilla 1.1N (Windows; I; 16bit)
-
-
- >> >I was trying to overload the "<<" operator for a Complex class,
- >> >without much sucess. Could someone please help me out ?
-
-
- >> you should put one line in your class definition:
- >> friend ostream& operator<<(ostream& s, Complex& z);
-
-
- It does not actually need to be a friend, not in this case, but this will
- solve the problem, which is that the header file gives no hint that the
- operator<< function exists.
-
- Also, you could rework your class to deal with constancy. For example,
- the operator<< function could take a const reference to complex;
-
- ostream& operator<<(ostream& s, const Complex& z);
-
- which will allow you to do this;
-
- cout << Complex( 5,5 ) << endl ;
-
-
- The Complex class, similarly, should have const member functions where
- appropriate.
-
-
- David.
-
-
-
-